package com.example.initializesql;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
/**
 * Created by 阿炜啊 on 2020/7/14.
 */

public class RlvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<Bean> list;
    private onItemClickListener onItemClickListener;
    private onItemLongClickListener onItemLongClickListener;

    public void setOnItemClickListener(RlvAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(RlvAdapter.onItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public RlvAdapter(Context context, ArrayList<Bean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_rlv, null);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.mTvId.setText(list.get(position).getId()+"");
        viewHolder.mTvName.setText(list.get(position).getName());
        viewHolder.mTvSex.setText(list.get(position).getSex());
        viewHolder.mTvAge.setText(list.get(position).getAge()+"");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemLongClickListener.onItemLongClick(position);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemLongClickListener.onItemLongClick(position);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static
    class ViewHolder extends RecyclerView.ViewHolder {
        View view;
        TextView mTvId;
        TextView mTvName;
        TextView mTvSex;
        TextView mTvAge;

        ViewHolder(View view) {
            super(view);
            this.view = view;
            this.mTvId = (TextView) view.findViewById(R.id.tv_id);
            this.mTvName = (TextView) view.findViewById(R.id.tv_name);
            this.mTvSex = (TextView) view.findViewById(R.id.tv_sex);
            this.mTvAge = (TextView) view.findViewById(R.id.tv_age);
        }
    }

    public interface onItemClickListener{
        void onitemClick(int position);
    }
    public interface onItemLongClickListener{
        void onItemLongClick(int position);
    }
}
